import string

from module_info import *
from module_troops import *

from process_common import *
from process_operations import *


max_inventory_items = 64
num_equipment_kinds = 11
num_weapon_proficiencies = 7


def save_troops():
  file = open(export_dir + "troops.txt","w")
  file.write("troopsfile version 1\n")
  file.write("%d "%len(troops))
  for troop in troops:
    if (troop[4] > 0):
      tag_uses[tag_scene][troop[4] & tsf_site_id_mask] = tag_uses[tag_scene][troop[4] & tsf_site_id_mask] + 1
      id_no = find_object(troops,convert_to_identifier(troop[0]))
      if (id_no >= 0):
        tag_uses[tag_troop][id_no] = tag_uses[tag_troop][id_no] + 1
    if (troop[6] > 0):
      tag_uses[tag_faction][troop[6]] = tag_uses[tag_faction][troop[6]] + 1
    first_upgrade = ((troop[3] & tf_upgrade_mask) >> tf_upgrade_bits)
    second_upgrade = ((troop[3] & tf_upgrade2_mask) >> tf_upgrade2_bits)
    if (first_upgrade > 0):
      tag_uses[tag_troop][first_upgrade] = tag_uses[tag_troop][first_upgrade] + 1
    if (second_upgrade > 0):
      tag_uses[tag_troop][second_upgrade] = tag_uses[tag_troop][second_upgrade] + 1
    
    file.write("\ntrp_%s %s %s %d %d %d %d\n  "%(convert_to_identifier(troop[0]),replace_spaces(troop[1]),replace_spaces(troop[2]),troop[3],troop[4],troop[5], troop[6]))
    inventory_list = troop[7]
#    inventory_list.append(itm_arrows)
#    inventory_list.append(itm_bolts)
    for inventory_item in inventory_list:
      tag_uses[tag_item][inventory_item] = tag_uses[tag_item][inventory_item] + 1
      file.write("%d 0 "%inventory_item)
    for i in xrange(max_inventory_items - len(inventory_list)):
      file.write("-1 0 ")
    file.write("\n ")
    attrib = troop[8]
    strength = (attrib & 0xff)
    agility  = ((attrib >> 8)& 0xff)
    intelligence = ((attrib >> 16)& 0xff)
    charisma = ((attrib >> 24)& 0xff)
    starting_level = (attrib >> level_bits) & level_mask
#    gold = two_to_pow(2 + (attrib >> 12)& 0x0f) * random
    
    file.write(" %d %d %d %d %d\n"%(strength,agility,intelligence,charisma,starting_level))
    wp_word = troop[9]
    for wp in xrange(num_weapon_proficiencies):
      wp_level = wp_word & 0x3FF
      file.write(" %d"%wp_level)
      wp_word = wp_word >> 10
    file.write("\n")
      
    skill_array = troop[10]
    for i in xrange(num_skill_words):
      file.write("%d "%((skill_array >> (i * 32)) & 0xffffffff))
    file.write("\n  ")

    face_keys = [0,0]
    if (len(troop) > 11):
      face_keys[0] = troop[11]
    if (len(troop) > 12):
      face_keys[1] = troop[12]
    for fckey in (face_keys):
      word1 = fckey & 0xFFFFFFFFFFFFFFFF
      word2 = (fckey >> 64) & 0xFFFFFFFFFFFFFFFF
      file.write("%d %d "%(word2,word1))

#    face_keys = troop[10]
#    for fckey in (face_keys):
#      file.write("%d "%(fckey))
#    for i in xrange(4 - len(face_keys)):
#      file.write("0 ")
      
    
  file.close()

def two_to_pow(x):
  result = 1
  for i in xrange(x):
    result = result * 2
  return result

def save_python_header():
  file = open("./ID_troops.py","w")
  for i_troop in xrange(len(troops)):
    file.write("trp_%s = %d\n"%(convert_to_identifier(troops[i_troop][0]),i_troop))
  file.close()

print "Exporting troops data"
tag_uses = load_tag_uses(export_dir)
save_python_header()
save_troops()
save_tag_uses(export_dir, tag_uses)
#print "Generating C header..."
#save_c_header()
#print "Generating Python header..."
#print "Finished."
